package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("FoodbatchadditiveController")
public class FoodbatchadditiveController extends CmiEmaController {

	@RequestMapping("/listFoodbatchadditives")
	public ModelAndView listFoodbatchadditives(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = foodbatchadditivePaginationService.updatePagination(request, pagination);
		Set<Foodbatchadditive> foodbatchadditives = foodbatchadditivePaginationService
				.getPaginationFoodbatchadditives(pagination);

		mav.addObject("foodbatchadditives", foodbatchadditives);
		mav.addObject("entityName", "foodbatchadditive");
		mav.addObject("pagination", pagination);
		mav.setViewName("foodbatchadditive/listFoodbatchadditives.jsp");
		return mav;
	}

	public String listFoodbatchadditives() {
		return "redirect:/listFoodbatchadditives";
	}

	@RequestMapping("/viewFoodbatchadditive")
	public ModelAndView viewFoodbatchadditive(@RequestParam Integer foodbatchadditiveId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("foodbatchadditive", foodbatchadditiveDAO.findFoodbatchadditiveByPrimaryKey(foodbatchadditiveId));
		mav.setViewName("foodbatchadditive/viewFoodbatchadditive.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteFoodbatchadditives")
	public ModelAndView confirmDeleteFoodbatchadditives(
			@RequestParam(value = "deleteFoodbatchadditivesIds", required = true) Set<Integer> deleteFoodbatchadditivesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Foodbatchadditive> deleteFoodbatchadditives = foodbatchadditiveDAO
				.findFoodbatchadditivesByIds(deleteFoodbatchadditivesIds);
		Set<Foodbatchadditive> referencedFoodbatchadditives = foodbatchadditiveService
				.loadReferenceFoodbatchadditives(deleteFoodbatchadditives);
		boolean hasReferenced;
		if (referencedFoodbatchadditives.size() == 0) {
			mav.addObject("deleteEntitys", deleteFoodbatchadditives);
			hasReferenced = false;
		} else {
			deleteFoodbatchadditives.removeAll(referencedFoodbatchadditives);
			mav.addObject("referencedEntitys", referencedFoodbatchadditives);
			mav.addObject("deleteEntitys", deleteFoodbatchadditives);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Foodbatchadditive");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listFoodbatchadditives");
		return mav;
	}

	@RequestMapping("/deleteFoodbatchadditive")
	public String deleteFoodbatchadditive(@RequestParam Integer foodbatchadditiveId) {
		Foodbatchadditive foodbatchadditive = foodbatchadditiveDAO
				.findFoodbatchadditiveByPrimaryKey(foodbatchadditiveId);
		foodbatchadditiveService.deleteFoodbatchadditive(foodbatchadditive);
		return "forward:/listFoodbatchadditives";
	}

	@RequestMapping("/confirmDeleteFoodbatchadditive")
	public ModelAndView confirmDeleteFoodbatchadditive(@RequestParam Integer foodbatchadditiveId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("foodbatchadditive", foodbatchadditiveDAO.findFoodbatchadditiveByPrimaryKey(foodbatchadditiveId));

		mav.setViewName("foodbatchadditive/deleteFoodbatchadditive.jsp");

		return mav;
	}

	@RequestMapping("/deleteFoodbatchadditives")
	public String deleteFoodbatchadditives(
			@RequestParam(value = "deleteFoodbatchadditivesId", required = true) Set<Integer> deleteFoodbatchadditivesId) {
		foodbatchadditiveService.deleteFoodbatchadditives(deleteFoodbatchadditivesId);
		return "forward:/listFoodbatchadditives";
	}

	@RequestMapping("/editFoodbatchadditive")
	public ModelAndView editFoodbatchadditive(@RequestParam Integer foodbatchadditiveId) {

		ModelAndView mav = new ModelAndView();

		Foodbatchadditive foodbatchadditive = foodbatchadditiveDAO
				.findFoodbatchadditiveByPrimaryKey(foodbatchadditiveId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("foodbatchadditive", foodbatchadditive);

		// 把所有的引用foodbatchadditive对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodbatchadditiveService.loadFoodbatchadditiveObjectReferencedSet(foodbatchadditive));

		// 将编辑页面foodbatchadditive添加到mav中
		mav.setViewName("foodbatchadditive/editFoodbatchadditive.jsp");
		return mav;
	}

	@RequestMapping("/newFoodbatchadditive")
	public ModelAndView newFoodbatchadditive() {
		ModelAndView mav = new ModelAndView();
		Foodbatchadditive foodbatchadditive = new Foodbatchadditive();

		mav.addObject("foodbatchadditive", foodbatchadditive);

		// 把所有的引用foodbatchadditive对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodbatchadditiveService.loadFoodbatchadditiveObjectReferencedSet(foodbatchadditive));

		mav.addObject("newFlag", true);
		mav.setViewName("foodbatchadditive/editFoodbatchadditive.jsp");

		return mav;
	}

	@RequestMapping("/saveFoodbatchadditive")
	public ModelAndView saveFoodbatchadditive(@ModelAttribute Foodbatchadditive foodbatchadditive) {

		ModelAndView mav = new ModelAndView();

		Additive additiveTemp = foodbatchadditive.getAdditive();
		Additive additive = additiveTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(additiveDAO.findAdditiveById(additiveTemp.getId()));
		Foodbatch foodbatchTemp = foodbatchadditive.getFoodbatch();
		Foodbatch foodbatch = foodbatchTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(foodbatchDAO.findFoodbatchById(foodbatchTemp.getId()));

		Foodbatchadditive existingFoodbatchadditive = CmiHibernateProxyUtil
				.deproxy(foodbatchadditiveDAO.findFoodbatchadditiveByPhysicalPrimaryKey(foodbatch.getId(),
						additive.getId(), foodbatchadditive.getContent()));

		foodbatchadditive.setAdditive(additive);
		foodbatchadditive.setFoodbatch(foodbatch);
		// 新建记录
		if (foodbatchadditive.getId() == null) {
			if (foodbatchadditive.equals(existingFoodbatchadditive)) {

				mav.addAllObjects(foodbatchadditiveService.loadFoodbatchadditiveClassReferencedSet(foodbatchadditive));
				// mav = editContinue(mav, foodbatchadditive);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("foodbatchadditive/editFoodbatchadditive.jsp");
			} else {
				foodbatchadditiveService.saveFoodbatchadditive(foodbatchadditive);
				mav.setViewName("forward:/listFoodbatchadditives");
			}
		}
		// 编辑记录
		else {
			Foodbatchadditive originFoodbatchadditive = CmiHibernateProxyUtil
					.deproxy(foodbatchadditiveDAO.findFoodbatchadditiveByPrimaryKey(foodbatchadditive.getId()));
			if (!foodbatchadditive.equals(originFoodbatchadditive)
					&& foodbatchadditive.equals(existingFoodbatchadditive)) {
				mav.addAllObjects(foodbatchadditiveService.loadFoodbatchadditiveClassReferencedSet(foodbatchadditive));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("foodbatchadditive/editFoodbatchadditive.jsp");
			} else {
				foodbatchadditiveService.saveFoodbatchadditive(foodbatchadditive);
				mav.setViewName("forward:/listFoodbatchadditives");
			}
		}
		return mav;
	}

	@RequestMapping("/foodbatchadditiveController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
